$(function(){
	//------------------------------------------------------------------------------------------------------------------
	//企业会员电话号码验证
	$.validator.addMethod("checkPhone", function(value, element, param){
		var phone = $("#" + param[0]).val() + '-' + value + '-' + $("#" + param[1]).val();
		phone = str_trim(phone);
		var regu = /(^[0-9]{2,5}\-[0-9]{7,9})((\-\d{0,10})?)$/g;
		if(regu.test(phone)){
			return true;
		}else{
			return false;
		}
	}, "请输入正确的电话号码！");
	//个人会员手机号码验证
	$.validator.addMethod("checkUsername", function(value, element) {
		var regu =/^(?=[\u4e00-\u9fa5a-zA-Z])(?!\d+)[\u4e00-\u9fa5\w.]{6,20}$/;
		if(regu.test(value)){
			return true;
		}else{
			return false;
		}
	}, "必须中英文开头6-20位数");

	//企业会员表单验证 使用jQuery Validate 插件进行验证
	$('#regForm').validate({
		//指定使用什么标签标记错误。
		errorElement: "span",
		//一个函数，可以自定义错误放到哪里。
		errorPlacement: function(error, element){
			$(element).parents('dl').find(".errinfo").append(error);
		},
		//自定义错误消息
		tipPlacement: function(element, tip){
			$(element).parents('dl').find(".errinfo").append(tip);
		},
		//通过验证后执行的函数
		success: function(label){
			if(label.parents('dl').find('dd input').val() != ''){
				label.text('ok!').addClass('success');
			}
		},
		//未验证后执行的函数
		highlight: function(element, errorClass){
			$(element).closest("dl").find("dd.errinfo").find('span').removeClass('success');
		},
		//js上面添加验证规则
		rules: {
			company_name: {
				required: true
			},
			contacts: {
				required: true
			},
			email: {
				email: true
			},
			phone: {
				required: true,
				checkPhone: ['area_code', 'ext']          //期望的是true,如果为false则展示提示信息
			},
			username: {
				required: true,
				minlength: 3,
				checkUsername:true, //期望的是true,如果为false则展示提示信息
				remote: {
					url: userUrl,                //后台处理程序
					type: "post",               //数据发送方式
					data: {                     //要传递的数据
						username: function(){ return $("#username").val();},
						_token: _token
					},
					dataType: "html",
					dataFilter: function(data, type){
						return (data == "true") ? true : false;
					}
				}
			},
			password: {
				required: true,
				minlength: 6
			},
			password_confirmation: {
				required: true,
				minlength: 6,
				equalTo: "#password"
			},
			code: {
				required: true,
				remote: {
					url: '/common/ajax_code',                //后台处理程序
					type: "post",               //数据发送方式
					data: {                     //要传递的数据
						code: function(){ return $("#code").val();},
						_token: _token
					},
					dataType: "html",
					dataFilter: function(data, type){
						return (data == "true") ? true : false;
					}
				}
			}
		},
		//错误信息提示修改
		messages: {
			company_name: {
				required: "请输入公司名称"
			},
			contacts: {
				required: "请输入联系人"
			},
			email: {
				email: "请输入一个正确的邮箱"
			},
			phone: {
				required: "请输入电话号码"
			},
			username: {
				required: "请输入用户名",
				minlength: "用户名必需由三个字符组成",
				remote: $.validator.format('用户名已经被注册')
			},
			password: {
				required: "请输入密码",
				minlength: "密码长度不能小于6个字母"
			},
			password_confirmation: {
				required: "请输入确认密码",
				minlength: "密码长度不能小于6个字母",
				equalTo: "两次密码输入不一致"
			},
			code: {
				required: "请输入验证码",
				remote: $.validator.format('验证码错误，请重新输入')
			}
		},
		submitHandler: function(form){
			form.submit();
		}
	});
	//$('form input[placeholder]').placeholder({isUseSpan:true});
});